package com.example.demo1.controller;

import lombok.RequiredArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.example.demo1.entity.User;
import com.example.demo1.service.UserService;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    
    private final UserService userService;

    @GetMapping("/test")
    public String test() {
        log.info("Test endpoint called");
        return "API is working!";
    }

    @PostMapping("/register")
    public ResponseEntity<?> register(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        try {
            System.out.println("收到注册请求：" + user.getUsername());
            
            // 清理不安全数据
            user.setId(null);
            user.setCreatedAt(null);
            user.setUpdatedAt(null);
            
            userService.register(user);
            
            response.put("success", true);
            response.put("message", "注册成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.out.println("注册失败：" + e.getMessage());
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        try {
            User loggedInUser = userService.login(user.getUsername(), user.getPassword());
            if (loggedInUser != null) {
                response.put("success", true);
                response.put("userId", loggedInUser.getId());
                response.put("username", loggedInUser.getUsername());
                response.put("major", loggedInUser.getMajor());
                response.put("className", loggedInUser.getClassName());
                response.put("isAdmin", loggedInUser.getIsAdmin());
                
                System.out.println("User login successful:");
                System.out.println("Username: " + loggedInUser.getUsername());
                System.out.println("IsAdmin: " + loggedInUser.getIsAdmin());
                
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "用户名或密码错误");
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    @PostMapping("/apply-admin/{userId}")
    public ResponseEntity<?> applyForAdmin(@PathVariable Long userId) {
        Map<String, Object> response = new HashMap<>();
        try {
            userService.setAdminRole(userId);
            response.put("success", true);
            response.put("message", "管理员权限设置成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 



